package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    public void clearImg() {

        //测试
        //System.out.println("系统清理了一次垃圾图片...");

        Set<String> sdiff =
                jedisPool.getResource().sdiff
                        (RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");

        if (sdiff != null) {
            for (String s : sdiff) {
                //先删除七牛云服务器中的图片
                QiniuUtils.deleteFileFromQiniu(s);

                //再删除redis数据库中的set集合
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, s);
                System.out.println("删除了一张垃圾图片");
            }
        }

        System.out.println(sdf.format(new Date()) + " 系统清理了一次垃圾图片");
    }
}
